package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.bean.Tree;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpOrganizationMapper;
import cc.rengu.redp.bizimpl.upmp.dao.UpmpUserInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrgTreeInfo;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpOrganization;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpOrganizationService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpOrganizationVO;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.TreeUtil;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
 * @author System
 */
@Service("upmpOrganizationService")
@DS("upmp")
@Slf4j
public class UpmpOrganizationServiceImpl extends ServiceImpl<UpmpOrganizationMapper, UpmpOrganization> implements IUpmpOrganizationService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private UpmpUserInfoMapper upmpUserInfoMapper;

    @Override
    public Map<String, Object> findOrgTrees(UpmpOrganizationVO organization) {
        Map<String, Object> result = new HashMap<>();
        try {
            LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
            findCondition(queryWrapper, organization);
            List<UpmpOrganization> organizations = baseMapper.selectList(queryWrapper);

            List<Tree<UpmpOrganization>> trees = new ArrayList<>();
            List<String> ids = new ArrayList<>();
            Map<String, String> idToCode = new LinkedHashMap<>();
            buildTrees(trees, organizations, ids, idToCode);

            result.put("ids", ids);
            result.put("idToCode", idToCode);
            Tree<UpmpOrganization> orgTree = TreeUtil.build(trees);
            result.put("rows", orgTree);

            result.put("total", organizations.size());
        } catch (NumberFormatException e) {
            log.error("查询机构失败", e);
            result.put("rows", null);
            result.put("total", 0);
        }
        return result;
    }

    /**
     * 根据机构编码获取机构及下属机构列表
     *
     * @return
     */
    public List<UpmpOrgTreeInfo> getAllOrgForTree(String code, String instId, List<UpmpOrgTreeInfo> result) {
        if (result.size() == 0) {
            UpmpOrganizationVO organizationVO = new UpmpOrganizationVO();
            organizationVO.setOrgCode(code);
            organizationVO.setInstId(instId);
            LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
            findCondition(queryWrapper, organizationVO);
            UpmpOrganization org = baseMapper.selectOne(queryWrapper);
            UpmpOrgTreeInfo upmpOrgTreeInfo = new UpmpOrgTreeInfo(org.getOrgCode(), org.getOrgName(), org.getParentOrgCode(), org.getOrgLevel());
            result.add(upmpOrgTreeInfo);
        }

        UpmpOrganizationVO organizationVO = new UpmpOrganizationVO();
        organizationVO.setParentOrgCode(code);
        organizationVO.setInstId(instId);
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        findCondition(queryWrapper, organizationVO);
        List<UpmpOrganization> list = baseMapper.selectList(queryWrapper);

        if (Common.isNotBlank(list) && list.size() > 0) {
            Iterator<UpmpOrganization> it = list.iterator();
            while (it.hasNext()) {
                UpmpOrganization org = it.next();
                UpmpOrgTreeInfo upmpOrgTreeInfo = new UpmpOrgTreeInfo(org.getOrgCode(), org.getOrgName(), org.getParentOrgCode(), org.getOrgLevel());
                result.add(upmpOrgTreeInfo);
                //
                getAllOrgForTree(org.getOrgCode(), org.getInstId(), result);
            }
        }
        return result;
    }

    private void buildTrees(List<Tree<UpmpOrganization>> trees, List<UpmpOrganization> organizations, List<String> ids, Map<String, String> idToCode) {
        organizations.forEach(organization -> {
            ids.add(organization.getId());
            idToCode.put(organization.getId(), organization.getOrgCode());
            Tree<UpmpOrganization> tree = new Tree<>();
            tree.setId(organization.getId());
            tree.setKey(tree.getId());
            tree.setParentId(organization.getParentId());
            tree.setText(organization.getOrgName());
            tree.setTitle(tree.getText());
            tree.setCreateTime(organization.getCreateTime());
            tree.setModifyTime(organization.getLastUpdateTime());
            tree.setOrder(ids.size());
            tree.setTitle(tree.getText());
            tree.setValue(tree.getId());
            tree.setInstId(organization.getInstId());
            tree.setOrgName(organization.getOrgName());
            tree.setOrgType(organization.getOrgType());
            tree.setOrgCode(organization.getOrgCode());
            tree.setOrgLevel(organization.getOrgLevel());
            tree.setOrgStatus(organization.getOrgStatus());
            tree.setParentOrgCode(organization.getParentOrgCode());

            trees.add(tree);
        });
    }

    private void findCondition(LambdaQueryWrapper<UpmpOrganization> queryWrapper, UpmpOrganizationVO organization) {
        if (StringUtils.isNotBlank(organization.getInstId())) {
            queryWrapper.eq(UpmpOrganization::getInstId, organization.getInstId());
        }else{
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            queryWrapper.eq(UpmpOrganization::getInstId, user.getInstId());
        }
        if (StringUtils.isNotBlank(organization.getOrgCode())) {
            queryWrapper.eq(UpmpOrganization::getOrgCode, organization.getOrgCode());
        }
        if (StringUtils.isNotBlank(organization.getOrgName())) {
            queryWrapper.eq(UpmpOrganization::getOrgName, organization.getOrgName());
        }
        if (StringUtils.isNotBlank(organization.getOrgType())) {
            queryWrapper.eq(UpmpOrganization::getOrgType, organization.getOrgType());
        }
        if (StringUtils.isNotBlank(organization.getParentOrgCode())) {
            queryWrapper.eq(UpmpOrganization::getParentOrgCode, organization.getParentOrgCode());
        }
        if (StringUtils.isNotBlank(organization.getParentId())) {
            queryWrapper.eq(UpmpOrganization::getParentId, organization.getParentId());
        }
        //orgStatus状态为01为正常，03为注销
        if (StringUtils.isNotBlank(organization.getOrgStatus())) {
            queryWrapper.eq(UpmpOrganization::getOrgStatus, organization.getOrgStatus());
        }
    }

    @Override
    public List<UpmpOrganization> findList(UpmpOrganizationVO organization) {
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        findCondition(queryWrapper, organization);
        queryWrapper.orderByAsc(UpmpOrganization::getCreateTime);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public void add(UpmpOrganizationVO organization) throws RedpException {

        UpmpOrganization newOrg = new UpmpOrganization();
        BeanUtils.copyProperties(organization, newOrg);
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        newOrg.setOprId(user.getId());
        newOrg.setCrtOrgId(user.getOrgCode());
        newOrg.setCreateTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        newOrg.setOrgName(organization.getOrgName().trim());
        organization.setCreateTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        newOrg.setOrgStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
        setparentOrgCode(newOrg);
        this.save(newOrg);
    }

    @Override
    public void update(UpmpOrganizationVO organization) throws RedpException {

        UpmpOrganization db = baseMapper.selectById(organization.getId());
        BeanUtils.copyProperties(organization, db);
        db.setLastUpdateTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        db.setLastOprId(user.getId());
        db.setOrgStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
        setparentOrgCode(db);
        baseMapper.updateById(db);
    }

    @Override
    public void delete(String[] orgIds) throws Exception {
//        this.baseMapper.deleteBatchIds(Arrays.asList(orgIds));
        if(orgIds.length>0) {
            UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
            for (String orgId : orgIds) {
                UpmpOrganization db = this.baseMapper.selectById(orgId);

                QueryWrapper<UpmpUserInfo> qwUser = new QueryWrapper<UpmpUserInfo>();
                qwUser.eq("inst_id",db.getInstId());
                qwUser.eq("org_code",db.getOrgCode());
                List<UpmpUserInfo> upmpUserInfos = upmpUserInfoMapper.selectList(qwUser);
                if(ObjectUtils.isEmpty(upmpUserInfos)){
                    db.setLastUpdateTime(DateUtil.getDateFormat(new Date(), DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
                    db.setLastOprId(user.getId());
                    db.setOrgStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                    this.baseMapper.updateById(db);
                    UpmpOrganizationVO organization = new UpmpOrganizationVO();
                    organization.setParentId(orgId);
                    LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
                    findCondition(queryWrapper, organization);
                    List<UpmpOrganization> organizations = baseMapper.selectList(queryWrapper);
                    List<String> subOrgIds = new ArrayList<>();
                    organizations.forEach(org -> {
                        subOrgIds.add(org.getId());
                    });
                    String[] y = subOrgIds.toArray(new String[subOrgIds.size()]);
                    delete(y);
                }else {
                    throw new RedpException("法人行号：["+db.getInstId()+"],机构号["+db.getOrgCode()+"]下已有用户不能删除!");
                }
            }
        }
    }

    /**
     * 根据用户及法人行获取机构
     *
     * @param user
     * @param instId 运营中心操作员选择的法人行、或者非运营中心操作员所属的法人行
     * @return
     */
    @Override
    public UpmpOrganization getOrgByUser(UpmpUserInfoVO user, String instId) {
        UpmpOrganization org;
        if (!user.isAdmin()) {//如果不是运营中心操作员，则直接通过用户信息取法人行
            org = getOrgByCode(user.getOrgCode(), user.getInstId());
        } else {//如果是运营中心操作员则取法人行总行
            org = getHeadOrgByCode(instId);
        }
        return org;
    }

    @Override
    public UpmpOrganization getOrgByCode(String code, String instId) {
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UpmpOrganization::getInstId, instId);
        queryWrapper.eq(UpmpOrganization::getOrgCode, code);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据法人行号获取总行,总行行号规则是 instId+"000000"
     *
     * @param instId
     * @return
     */
    //TODO 后续增加发法人行此方法需要更改
    @Override
    public UpmpOrganization getHeadOrgByCode(String instId) {
        String orgCode = instId + "000000";
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UpmpOrganization::getInstId, instId);
        queryWrapper.eq(UpmpOrganization::getOrgCode, orgCode);
        return baseMapper.selectOne(queryWrapper);
//        if(!IGAS_DEFAULT_INST_ID.equals(instId)){
//            //通过分行查分行上级机构，将分行上级机构作为总行
//            List<UpmpOrganization> branchOrgByCode = organizationDao.getHeadOrgByCode(OrgLevelEnum.BRANCH.getType(),instId);
//            if(CollectionUtils.isNotEmpty(branchOrgByCode)){
//                UpmpOrganization branchOrg = branchOrgByCode.get(0);
//                UpmpOrganization headOrg = organizationDao.getOrgByCode(branchOrg.getSuperOrgCode(),instId);
//                return headOrg;
//            }
//        }else {
//            List<UpmpOrganization> headOrgByCode = organizationDao.getHeadOrgByCode(OrgLevelEnum.HEAD.getType(),instId);
//            if(CollectionUtils.isNotEmpty(headOrgByCode)){
//                return headOrgByCode.get(0);
//            }
//        }
//        return null;
    }

    @Override
    public UpmpOrganization findOneByCode(String signInstId, String status, String instId) {
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UpmpOrganization::getInstId, instId);
        queryWrapper.eq(UpmpOrganization::getOrgCode, signInstId);
        queryWrapper.eq(UpmpOrganization::getOrgStatus, status);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询所有机构
     *
     * @param request
     * @return
     */
    @Override
    public Map<String, Object> organizationSelect2List(QueryRequest request, UpmpOrganization upmp) {

        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String userOrgCodes = user.getOrgCode();
        LambdaQueryWrapper<UpmpOrganization> wrapper = new LambdaQueryWrapper<>();
        //q的格式为  EQ_instId & CN_feeCd_OR_feeName & EQ_pCalType
/*        q = q.replaceAll("&amp;", "&");
        if (null == q) {
            return null;
        }
        String instId = "";
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            wrapper.or(wp -> wp.likeRight(UpmpOrganization::getOrgName, param[0]).or().likeRight(UpmpOrganization::getOrgCode, param[0]));
        }
        if (null != param && param.length > 1) {
            if (Common.isBlank(param[1]) && Common.isNotBlank(user.getInstId())) {
                instId = user.getInstId();
            } else {
                if (user.isAdmin()) {
                    instId = param[1];
                } else {
                    instId = user.getInstId();
                }
            }
        } else {
            instId = user.getInstId();
        }*/
        if (Common.isNotBlank(upmp.getInstId())) {
            wrapper.eq(UpmpOrganization::getInstId, upmp.getInstId());
        } else {
            log.info("没有获取到法人行信息，无法查询机构数据");
            return null;
        }
        if (!user.isAdmin()) {
//            String userOrgCodes = GZIPUtils.uncompressToString(user.getUserOrgCodes());
            if (null != userOrgCodes && userOrgCodes.length() > 0) {
                wrapper.eq(UpmpOrganization::getOrgCode, userOrgCodes);
            }
        }
        if (Common.isNotBlank(upmp.getInstId())) {
            wrapper.or(wp -> wp.likeRight(UpmpOrganization::getOrgName,upmp.getOrgCode()).or().likeRight(UpmpOrganization::getOrgCode,upmp.getOrgCode()));
        }
        Page<UpmpOrganization> page = new Page<>();
        SortUtil.handlePageSort(request, page, true);
        Page<UpmpOrganization> page1 = this.page(page, wrapper);
        List<UpmpOrganization> listx = page1.getRecords();
        Map<String, Object> maps = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        if (listx != null) {
            for (int i = 0; i < listx.size(); i++) {
                UpmpOrganization org = listx.get(i);
                map = new HashMap<String, String>();
                map.put("id", org.getOrgCode());
                map.put("name", org.getOrgName());
                map.put("unionOrgcode", org.getUpayOrgCode());
                map.put("bankBranchCode", org.getBankBranchCode());
                map.put("level", org.getOrgLevel());
                list.add(map);
            }
            maps.put("rows", list);
            maps.put("total", page.getTotal());
            int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
            if (pageIndex == page1.getTotal()) {
                maps.put("more", false);
            } else {
                maps.put("more", true);
            }
        }
        return maps;
    }

    @Override
    public Map<String, Object> orgoneSelect2List(QueryRequest request, String q) {
        LambdaQueryWrapper<UpmpOrganization> wrapper = new LambdaQueryWrapper<>();
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        //q的格式为  EQ_instId & CN_feeCd_OR_feeName & EQ_pCalType
        q = q.replaceAll("&amp;", "&");
        if (null == q) {
            return null;
        }
        String instId = "";
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            wrapper.or(wp -> wp.likeRight(UpmpOrganization::getOrgName, param[0]).likeRight(UpmpOrganization::getOrgCode, param[0]));
        }

        Page<UpmpOrganization> page = new Page<>();
        SortUtil.handlePageSort(request, page, true);
        Page<UpmpOrganization> page1 = this.page(page, wrapper);
        List<UpmpOrganization> listx = page1.getRecords();
        Map<String, Object> maps = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        if (listx != null) {
            for (int i = 0; i < listx.size(); i++) {
                UpmpOrganization org = listx.get(i);
                map = new HashMap<String, String>();
                map.put("id", org.getOrgCode());
                map.put("name", org.getOrgName());
                map.put("unionOrgcode", org.getUpayOrgCode());
                map.put("bankBranchCode", org.getBankBranchCode());
                map.put("level", org.getOrgLevel());
                list.add(map);
            }
            maps.put("results", list);
            maps.put("total", page.getTotal());
            int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
            if (pageIndex == page1.getTotal()) {
                maps.put("more", false);
            } else {
                maps.put("more", true);
            }
        }
        return maps;
    }

    @Override
    public List<UpmpOrganization> getOrgListByParentCode(String parentCode, String instId) {
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UpmpOrganization::getInstId, instId);
        queryWrapper.eq(UpmpOrganization::getParentOrgCode, parentCode);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public String selectOrgByCode(String code) {
        LambdaQueryWrapper<UpmpOrganization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UpmpOrganization::getParentOrgCode, code);
        List<UpmpOrganization> list = baseMapper.selectList(queryWrapper);
        if (list.size() == 0) {
            return "";
        }
        return list.get(0).getOrgName();
    }

    @Override
    public Map<String, Object> organizationSelectList(QueryRequest request, UpmpOrganization organization) {


        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        LambdaQueryWrapper<UpmpOrganization> wrapper = new LambdaQueryWrapper<>();

        if (Common.isNotBlank(organization.getOrgCode())) {
            wrapper.or(wp -> wp.likeRight(UpmpOrganization::getOrgName, organization.getOrgCode()).or().likeRight(UpmpOrganization::getOrgCode, organization.getOrgCode()));
        }
        String instId = "";

        if (Common.isBlank(organization.getInstId()) && Common.isNotBlank(user.getInstId())) {
            instId = user.getInstId();
        } else {
            if (user.isAdmin()) {
                instId = organization.getInstId();
            } else {
                instId = user.getInstId();
            }
        }

        if (Common.isNotBlank(instId)) {
            wrapper.eq(UpmpOrganization::getInstId, instId);
        } else {
            log.info("没有获取到法人行信息，无法查询机构数据");
            return null;
        }

        if (!user.isAdmin()) {
//            String userOrgCodes = GZIPUtils.uncompressToString(user.getUserOrgCodes());
            String userOrgCodes = "";
            if (null != userOrgCodes && userOrgCodes.length() > 0) {
                wrapper.eq(UpmpOrganization::getOrgCode, userOrgCodes);
            }
        }
        Page<UpmpOrganization> page = new Page<>();
        SortUtil.handlePageSort(request, page, true);
        Page<UpmpOrganization> page1 = this.page(page, wrapper);
        List<UpmpOrganization> listx = page1.getRecords();
        Map<String, Object> maps = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        if (listx != null) {
            for (int i = 0; i < listx.size(); i++) {
                UpmpOrganization org = listx.get(i);
                map = new HashMap<String, String>();
                map.put("id", org.getOrgCode());
                map.put("name", org.getOrgName());
                map.put("unionOrgcode", org.getUpayOrgCode());
                map.put("bankBranchCode", org.getBankBranchCode());
                map.put("level", org.getOrgLevel());
                list.add(map);
            }
            maps.put("rows", list);
            maps.put("total", page.getTotal());
            int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
            if (pageIndex == page1.getTotal()) {
                maps.put("more", false);
            } else {
                maps.put("more", true);
            }
        }
        return maps;

    }

    /**
     * 上级机构赋值
     * @param db
     */
    private void setparentOrgCode(UpmpOrganization db) {
        UpmpOrganization orgByCode = getOrgByCode(db.getParentOrgCode(), db.getInstId());
        if(!ObjectUtils.isEmpty(orgByCode)){
            switch (db.getOrgLevel()) {
                case "00":
                    db.setFirstOrgCode(db.getOrgCode());
                    break;
                case "01":
                    db.setFirstOrgCode(db.getParentOrgCode());
                    db.setSecondOrgCode(db.getOrgCode());
                    break;
                case "02":
                    db.setFirstOrgCode(Common.isBlank(orgByCode.getFirstOrgCode()) ? "" : orgByCode.getFirstOrgCode());
                    db.setSecondOrgCode(Common.isBlank(orgByCode.getSecondOrgCode()) ? "" : orgByCode.getSecondOrgCode());
                    db.setThreeOrgCode(db.getOrgCode());
                    break;
                case "03":
                    db.setFirstOrgCode(Common.isBlank(orgByCode.getFirstOrgCode()) ? "" : orgByCode.getFirstOrgCode());
                    db.setSecondOrgCode(Common.isBlank(orgByCode.getSecondOrgCode()) ? "" : orgByCode.getSecondOrgCode());
                    db.setThreeOrgCode(Common.isBlank(orgByCode.getThreeOrgCode()) ? "" : orgByCode.getThreeOrgCode());
                    db.setFourOrgCode(db.getOrgCode());
                    break;
                case "04":
                    db.setFirstOrgCode(Common.isBlank(orgByCode.getFirstOrgCode()) ? "" : orgByCode.getFirstOrgCode());
                    db.setSecondOrgCode(Common.isBlank(orgByCode.getSecondOrgCode()) ? "" : orgByCode.getSecondOrgCode());
                    db.setThreeOrgCode(Common.isBlank(orgByCode.getThreeOrgCode()) ? "" : orgByCode.getThreeOrgCode());
                    db.setFourOrgCode(Common.isBlank(orgByCode.getOrgCode()) ? "" : orgByCode.getOrgCode());
                    db.setFiveOrgCode(db.getOrgCode());
                    break;
                case "05":
                    db.setFirstOrgCode(Common.isBlank(orgByCode.getFirstOrgCode()) ? "" : orgByCode.getFirstOrgCode());
                    db.setSecondOrgCode(Common.isBlank(orgByCode.getSecondOrgCode()) ? "" : orgByCode.getSecondOrgCode());
                    db.setThreeOrgCode(Common.isBlank(orgByCode.getThreeOrgCode()) ? "" : orgByCode.getThreeOrgCode());
                    db.setFourOrgCode(Common.isBlank(orgByCode.getFourOrgCode()) ? "" : orgByCode.getFourOrgCode());
                    db.setFiveOrgCode(Common.isBlank(orgByCode.getFiveOrgCode()) ? "" : orgByCode.getFiveOrgCode());
                    db.setSixOrgCode(db.getOrgCode());
                    break;
                case "06":
                    db.setFirstOrgCode(Common.isBlank(orgByCode.getFirstOrgCode()) ? "" : orgByCode.getFirstOrgCode());
                    db.setSecondOrgCode(Common.isBlank(orgByCode.getSecondOrgCode()) ? "" : orgByCode.getSecondOrgCode());
                    db.setThreeOrgCode(Common.isBlank(orgByCode.getThreeOrgCode()) ? "" : orgByCode.getThreeOrgCode());
                    db.setFourOrgCode(Common.isBlank(orgByCode.getFourOrgCode()) ? "" : orgByCode.getFourOrgCode());
                    db.setFiveOrgCode(Common.isBlank(orgByCode.getFiveOrgCode()) ? "" : orgByCode.getFiveOrgCode());
                    db.setSixOrgCode(Common.isBlank(orgByCode.getSixOrgCode()) ? "" : orgByCode.getSixOrgCode());
                    db.setSevenOrgCode(db.getOrgCode());
                    break;
            }
        }
    }
}
